package paidaxing.blog.post.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import paidaxing.blog.common.result.PageResult;
import paidaxing.blog.post.dto.PostDTO;
import paidaxing.blog.post.dto.request.PostQueryRequest;
import paidaxing.blog.post.dto.request.TagRequestDTO;
import paidaxing.blog.post.dto.response.TagResponseDTO;
import paidaxing.blog.post.entity.Tags;
import paidaxing.blog.post.mapper.TagsDao;
import paidaxing.blog.post.service.TagsService;
import paidaxing.blog.post.service.PostsService;
import paidaxing.blog.post.utils.RequestConverter;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 标签服务实现类
 */
@Service("tagsService")
public class TagsServiceImpl implements TagsService {
    @Autowired
    private TagsDao tagsDao;
    
    @Autowired
    private PostsService postsService;

    @Override
    public PageResult<TagResponseDTO> getTags(Integer page, Integer size) {
        int offset = (page - 1) * size;
        List<Tags> tagsList = tagsDao.queryAllByLimit(offset, size);
        long total = tagsDao.count();
        
        List<TagResponseDTO> tagDTOs = tagsList.stream()
                .map(RequestConverter::convertToTagResponseDTO)
                .collect(Collectors.toList());
        
        PageResult<TagResponseDTO> pageResult = new PageResult<>();
        pageResult.setRecords(tagDTOs);
        pageResult.setTotal((int) total);
        pageResult.setPageNo(page);
        pageResult.setPageSize(size);
        
        return pageResult;
    }
    
    @Override
    public TagResponseDTO createTag(TagRequestDTO tagRequestDTO) {
        Tags tag = RequestConverter.convertToTagEntity(tagRequestDTO);
        tag.setCreatedAt(new Date());
        tag.setUpdatedAt(new Date());
        tag.setDeleted(0);
        tag.setPostCount(0);
        tagsDao.insert(tag);
        
        return RequestConverter.convertToTagResponseDTO(tag);
    }
    
    @Override
    public TagResponseDTO updateTag(Long tagId, TagRequestDTO tagRequestDTO) {
        Tags existingTag = tagsDao.queryById(tagId);
        if (existingTag == null || existingTag.getDeleted() == 1) {
            return null;
        }
        
        Tags tag = RequestConverter.convertToTagEntity(tagRequestDTO);
        tag.setId(tagId);
        tag.setUpdatedAt(new Date());
        tagsDao.update(tag);
        
        return RequestConverter.convertToTagResponseDTO(tag);
    }
    
    @Override
    public boolean deleteTag(Long tagId) {
        Tags existingTag = tagsDao.queryById(tagId);
        if (existingTag == null || existingTag.getDeleted() == 1) {
            return false;
        }
        
        // 逻辑删除
        return tagsDao.deleteById(tagId) > 0;
    }
    
    @Override
    public PageResult<PostDTO> getPostsByTag(Long tagId, Integer page, Integer size) {
        Tags tag = tagsDao.queryById(tagId);
        if (tag == null || tag.getDeleted() == 1) {
            PageResult<PostDTO> emptyResult = new PageResult<>();
            emptyResult.setPageNo(page);
            emptyResult.setPageSize(size);
            emptyResult.setTotal(0);
            emptyResult.setRecords(java.util.Collections.emptyList());
            return emptyResult;
        }
        
        PostQueryRequest queryRequest = new PostQueryRequest();
        queryRequest.setTagId(tagId);
        queryRequest.pageNo = page;
        queryRequest.pageSize = size;
        
        return postsService.listPosts(queryRequest);
    }
}